package com.example.miniblog.controller;

import com.example.miniblog.entity.LoginForm;
import com.example.miniblog.entity.R;
import com.example.miniblog.entity.User;
import com.example.miniblog.service.UserService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //  增加用户
    @PostMapping("/add")
    public R add(@RequestBody User user){
        if(userService.add(user)){
            return R.ok();
        }else{
            return R.error();
        }
    }
    //  得到所有用户信息
    @GetMapping("/getAll")
    public R getAll(){
        return R.ok().put("tableData",userService.getAll());
    }

    //  通过id得到用户信息
    @GetMapping("/getUserById/{id}")
    public R getUserById(@PathVariable String id){
        return R.ok().put("tableData",userService.getUserById(id));
    }

    //  实现用户登录
    @PostMapping("/login")
    public R login(@RequestBody LoginForm loginForm){
        if(loginForm!= null){
            System.out.println("loginForm = " + loginForm);
            if(userService.login(loginForm)){
                return R.ok();
            }else {
                return R.error();
            }
        }
        return R.error();
    }
    //  实现用户退出
    @GetMapping("/logout")
    public R logout(@RequestBody LoginForm loginForm){
        System.out.println("退出loginForm = " + loginForm);
        return R.ok();
    }

    //  通过id删除用户
    @DeleteMapping("removeUser/{id}")
    public R removeUser(@PathVariable String id){
        if(userService.removeUser(id)){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //  跟新用户信息
    @PostMapping("updateUser")
    public R updateUser(@RequestBody User user){
        if (userService.updateUser(user)){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
